package ph.com.gs3.formalistics.view.custom.fields;

import ph.com.gs3.formalistics.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BarcodeScannerField extends LinearLayout {

	public static final String TAG = BarcodeScannerField.class.getSimpleName();

	private TextView tvLabel;
	private EditText etScannedCode;
	private Button bScanCode;

	private String id;
	private Context context;

	private BarcodeScannerListener listener;

	public BarcodeScannerField(Context context) {
		super(context);

		this.context = context;

		initialize();
	}

	private void initialize() {

		LayoutInflater.from(context).inflate(R.layout.field_barcode_scanner, this, true);

		tvLabel = (TextView) findViewById(R.id.Barcode_tvLabel);
		etScannedCode = (EditText) findViewById(R.id.Barcode_etScannedCode);
		bScanCode = (Button) findViewById(R.id.Barcode_bScanCode);
		bScanCode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (listener != null) {
					listener.onScanCodeCommand(BarcodeScannerField.this);
				}
			}
		});

	}

	// =================================================================================
	// {{ Getters & Setters

	public String getFieldId() {
		return id;
	}

	public void setFieldId(String id) {
		this.setTag(id + "_container");
		this.etScannedCode.setTag(id);
		this.id = id;
	}

	@Override
	public void setEnabled(boolean enabled) {

		etScannedCode.setEnabled(enabled);
		bScanCode.setEnabled(enabled);
		bScanCode.setClickable(enabled);

	}

	public void setLabel(String label) {
		tvLabel.setText(label);
	}

	public String getLabel() {
		return tvLabel.getText().toString();
	}

	public void setScannedCode(String scannedCode) {
		etScannedCode.setText(scannedCode);
	}

	public String getScannedCode() {
		return etScannedCode.getText().toString();
	}

	// }}

	// =====================================================================
	// {{ Interface(s)

	public BarcodeScannerListener getListener() {
		return listener;
	}

	public void setListener(BarcodeScannerListener listener) {
		this.listener = listener;
	}

	public static interface BarcodeScannerListener {

		public void onScanCodeCommand(BarcodeScannerField source);

	}

	// }}

}
